<?php

class Application_View_Helper_AdvContent extends Zend_View_Helper_Abstract {

    public function advContent($adv, $width, $height) {
        $type = $adv['type'];
        $html = '';
        if ($type == Application_Model_DAO_Adv::TYPE_HTML) {
            $html = $adv['content'];
        } elseif ($type == Application_Model_DAO_Adv::TYPE_IMG) {
            $content = $adv['content'];
            if (stripos($content, Application_Model_DAO_Adv::IMG_PREFIX) !== false) {
                $content = substr($content, strlen(Application_Model_DAO_Adv::IMG_PREFIX));
                if (isset($adv['img_code'])) {
                    $imgCode = $adv['img_code'];
                } else {
                    $daoImage = new Application_Model_DAO_Image();
                    $img = $daoImage->get($content);
                    $imgCode = $img['code'];
                }
                $content = $this->view->url(array('id' => $imgCode, 'width' => $width, 'height' => $height), 'image', true);
                $url = $adv['url'];
                if (!empty($url)) {
                    $html = '<a href="' . $this->view->url(array('controller' => 'adv', 'action' => 'goto', 'id' => $adv['code']), 'default', true) . '" target="_blank">';
                }
                $html .= '<img class="image-adv" src="' . $content . '" alt="" />';
                if (!empty($url)) {
                    $html .= '</a>';
                }
            }
        }
        return $html;
    }

}